﻿using System.Windows.Forms;
using System;

namespace Library.Winform.Draw.Tools {
	public class ToolDrawPolygon : ToolDraw {
		protected static Type ToolDrawPolygonType = typeof(ToolDrawPolygon);
		protected int LastX, LastY, MinDistance;
		public ToolDrawPolygon() {
			MinDistance = 15 * 15;
			LastY = 0;
			LastX = 0;
			Cursor = new Cursor(ToolDrawPolygonType, "Pencil.cur");
		}
		protected PolygonDraw CurrentPolygon;
		#region Overrides of ToolDraw

		protected override ObjectDraw CreateObjectOnMouseDown(IDrawContainer container, ScrollMouseEventArgs e) {
			CurrentPolygon = new PolygonDraw(e.X, e.Y, e.X + 1, e.Y + 1);
			return CurrentPolygon;
		}

		protected override ushort PointHandlerOnMouseMove(ScrollMouseEventArgs e) {
			//Point point = new Point(e.X, e.Y);
			var dx = e.X - LastX;
			var dy = e.Y - LastY;
			int distance = dx * dx + dy * dy;

			if (distance < MinDistance) {
				// Distance between last two points is less than minimum -
				// move last point
				if (CurrentPolygon.PointCount > 0)
					return (ushort)CurrentPolygon.PointCount;
				return 0;

			}

			// Add new point
			CurrentPolygon.AddPoint(e.X, e.Y);
			LastX = e.X;
			LastY = e.Y;
			return 0;

		}

		#endregion
	}
}
